#!/bin/bash

UX_INSTALL_DIR="/segfs/tango/ci/Tango"
WIN_INSTALL_DIR="//unixhome/segfs/tango/ci/Tango"

DS_NAME=devTest
FWD_DS_NAME=FwdTest

#
# Is clean required ?
#

if [ $# = 1 ]
then
	if [ $1 = "clean" ]
	then
		CLEAN="clean"
	fi
fi

#
# Set some variables according to OS
#

for i in $NODE_LABELS
do
	echo $i

	if [ $i = "ubuntu12.04" ]
	then
		echo "Ubuntu 12.04 OS"
		OS_TYPE="Linux"
		OS="ubuntu1204"
		MAKE_PATH="make"
		DS_INST_NAME=ubuntu1204
		DS_INST_NAME_2=ubuntu1204_2
		MCAST_INST_NAME=ubuntu1204_mcast
		INSTALL_DIR=$UX_INSTALL_DIR
		PROF=jenkins_no_mcast
	fi

	if [ $i = "Windows_VC9" ]
	then
		echo "Windows VC9"
		OS_TYPE="Windows"
		OS_SPEC="Windows"
		MAKE_PATH="devenv"
		DS_INST_NAME=win32_vc9
		DS_INST_NAME_2=win32_vc9_2
		MCAST_INST_NAME=win32_vc9_mcast
		INSTALL_DIR=$WIN_INSTALL_DIR
		BIN_DIR="win32"
		OUTFILE="devenv_suite"
	fi

	if [ $i = "Windows_VC10" ]
	then
		echo "Windows VC10 (64 bits)"
		OS_TYPE="Windows"
		OS_SPEC="Windows_64_VC10"
		MAKE_PATH="MSBuild.exe"
		DS_INST_NAME=win64_vc10
		DS_INST_NAME_2=win64_vc10_2
		MCAST_INST_NAME=win64_vc10_mcast
		INSTALL_DIR=$WIN_INSTALL_DIR
		BIN_DIR="win64"
		OUTFILE="msbuild_suite"
	fi
	
	if [ $i = "centos5" ]
	then
		echo "Centos 5 OS"
		OS_TYPE="Linux"
		OS="centos5"
		MAKE_PATH="make"
		DS_INST_NAME=centos5
		DS_INST_NAME_2=centos5_2
		MCAST_INST_NAME=centos5_mcast
		INSTALL_DIR=$UX_INSTALL_DIR
		PROF=jenkins_no_mcast
	fi

	if [ $i = "redhate4" ]
	then
		echo "Red Hat 4 OS"
		OS_TYPE="Linux"
		OS="redhate4"
		MAKE_PATH="make"
		DS_INST_NAME=redhate4
		DS_INST_NAME_2=redhate4_2
		MCAST_INST_NAME=redhate4_mcast
		INSTALL_DIR=$UX_INSTALL_DIR
		PROF=jenkins_no_mcast
	fi

	if [ $i = "redhate5" ]
	then
		echo "Red Hat 5 OS (64 bits)"
		OS_TYPE="Linux"
		OS="redhate5"
		MAKE_PATH="make"
		DS_INST_NAME=redhate5
		DS_INST_NAME_2=redhate5_2
		MCAST_INST_NAME=redhate5_mcast
		INSTALL_DIR=$UX_INSTALL_DIR
		PROF=jenkins_no_mcast
	fi

	if [ $i = "debian6" ]
	then
		echo "Debian 6 OS (64 bits)"
		OS_TYPE="Linux"
		OS="debian6"
		MAKE_PATH="make"
		DS_INST_NAME=debian6
		DS_INST_NAME_2=debian6_2
		MCAST_INST_NAME=debian6_mcast
		INSTALL_DIR=$UX_INSTALL_DIR
		PROF=jenkins_no_mcast
	fi

	if [ $i = "debian7_tango" ]
	then
		echo "Debian 7 OS (64 bits)"
		OS_TYPE="Linux"
		OS="debian7"
		MAKE_PATH="make"
		DS_INST_NAME=debian7
		DS_INST_NAME_2=debian7_2
		MCAST_INST_NAME=debian7_mcast
		INSTALL_DIR=$UX_INSTALL_DIR
		PROF=jenkins_no_mcast
	fi

	if [ $i = "debian8_tango" ]
	then
		echo "Debian 8 OS (64 bits)"
		OS_TYPE="Linux"
		OS="debian8"
		MAKE_PATH="make"
		DS_INST_NAME=debian8
		DS_INST_NAME_2=debian8_2
		MCAST_INST_NAME=debian8_mcast
		INSTALL_DIR=$UX_INSTALL_DIR
		PROF=jenkins_no_mcast
	fi
done

#
# Build make command
#

if [ $OS_TYPE = "Windows" ]
then
	if [ -z $CLEAN ]
	then
		if [ $OS_SPEC = "Windows_64_VC10" ]
		then
			BUILD="Build"
		else
			BUILD="/build"
		fi
	else
		if [ $OS_SPEC = "Windows_64_VC10" ]
		then
			BUILD="Clean;Build"
		else
			BUILD="/rebuild"
		fi
	fi
	export PREFIX=$INSTALL_DIR
	if [ $OS_SPEC = "Windows_64_VC10" ]
	then
		MAKE_CMD="$MAKE_PATH test_suite.sln /m /p:Platform=x64 /t:$BUILD /p:Configuration=Release /v:quiet /flp:LogFile=$OUTFILE;Summary;ShowCommandLine;Verbosity=minimal"
		export PATH=$PATH:/cygdrive/c/Windows/Microsoft.Net/Framework64/v4.0.30319
		cd cpp_test_suite/win64/vc10/test_suite
	else
		MAKE_CMD="$MAKE_PATH test_suite.sln $BUILD Release /out $OUTFILE "
		export PATH=$PATH:/cygdrive/c/Program\ Files/Microsoft\ Visual\ Studio\ 9.0/Common7/IDE
		cd cpp_test_suite/win32/vc9/test_suite
	fi
	PATH=$PATH:/bin
	echo $PATH
	rm -f $OUTFILE
else
	MAKE_CMD="$MAKE_PATH PROFILE=$PROF OS=$OS $CLEAN all"
	cd cpp_test_suite
fi

#
# Create export_variables script
# (exports all environmental variables necessary to execute particular tests)
#

if [ $OS_TYPE != "Windows" ]
then
	EXPORT_VARIABLES=$INSTALL_DIR/$OS/bin/export_variables
	
	if [ -f $EXPORT_VARIABLES ]
	then
		rm -f $EXPORT_VARIABLES
	fi
	
	echo "#!/bin/bash" > $EXPORT_VARIABLES
	echo "export LD_LIBRARY_PATH=$INSTALL_DIR/$OS/lib/debug:/segfs/tango/transport/zmq4.0.5/$OS/lib:/segfs/tango/tools/log4tango-release/log4tango5.0.1/$OS/lib:/segfs/tango/ORB/omniORB4.2.1/$OS/lib:$LD_LIBRARY_PATH" >> $EXPORT_VARIABLES 
	echo "export TANGO_HOST=acudebian7:10000" >> $EXPORT_VARIABLES
fi



#
# Run make
#

echo $MAKE_CMD
$MAKE_CMD
RET=$?
echo "Compilation done"
echo "OS = $OS"
echo "RET = $RET"

if [ $OS_TYPE = "Windows" ]
then
	cat $OUTFILE
	cd ../../..
fi

if [ $RET != 0 ]
then
	echo "Build fail!!!"
	exit -1
fi

#
# Now Start the first device server
#

TANGO_LIB=$INSTALL_DIR/$OS/lib
LOG4TANGO_LIB=/segfs/tango/tools/log4tango-release/log4tango5.0.1/$OS/lib
ORB_LIB=/segfs/tango/ORB/omniORB4.2.1/$OS/lib
ZMQ_LIB=/segfs/tango/transport/zmq4.0.5/$OS/lib

export LD_LIBRARY_PATH=$TANGO_LIB:$LOG4TANGO_LIB:$ORB_LIB:$ZMQ_LIB:$LD_LIBRARY_PATH
export TANGO_HOST=acudebian7:10000

if [ $OS_TYPE = "Windows" ]
then
	if [ $OS_SPEC = "Windows_64_VC10" ]
	then
#
# Since VMWare has been installed on the Windows 64 bits host, the host addresses
# are first local addresses (192.168.X.Y). Force server to listen and publish
# the real IP address until this problem is analysed.
#
#		$WIN_INSTALL_DIR/$BIN_DIR/bin/archive/$DS_NAME $DS_INST_NAME  -ORBendPoint giop:tcp:160.103.5.4 1>/dev/null 2>&1 &
#		$WIN_INSTALL_DIR/$BIN_DIR/bin/archive/$DS_NAME $DS_INST_NAME_2  -ORBendPoint giop:tcp:160.103.5.4 1>/dev/null 2>&1 &
#		$WIN_INSTALL_DIR/$BIN_DIR/bin/archive/$DS_NAME $MCAST_INST_NAME  -ORBendPoint giop:tcp:160.103.5.4 1>/dev/null 2>&1 &
		$WIN_INSTALL_DIR/$BIN_DIR/bin/archive/$DS_NAME $DS_INST_NAME 1>/dev/null 2>&1 &
		$WIN_INSTALL_DIR/$BIN_DIR/bin/archive/$DS_NAME $DS_INST_NAME_2 1>/dev/null 2>&1 &
		sleep 2
		$WIN_INSTALL_DIR/$BIN_DIR/bin/archive/$FWD_DS_NAME $DS_INST_NAME 1>/dev/null 2>&1 &
		$WIN_INSTALL_DIR/$BIN_DIR/bin/archive/$DS_NAME $MCAST_INST_NAME 1>/dev/null 2>&1 &
	else
		$WIN_INSTALL_DIR/$BIN_DIR/bin/archive/$DS_NAME $DS_INST_NAME  1>/dev/null 2>&1 &
		$WIN_INSTALL_DIR/$BIN_DIR/bin/archive/$DS_NAME $DS_INST_NAME_2  1>/dev/null 2>&1 &
		sleep 2
		$WIN_INSTALL_DIR/$BIN_DIR/bin/archive/$FWD_DS_NAME $DS_INST_NAME  1>/dev/null 2>&1 &
		$WIN_INSTALL_DIR/$BIN_DIR/bin/archive/$DS_NAME $MCAST_INST_NAME  1>/dev/null 2>&1 &
	fi
else
	echo "Shared libs used by device server"
	ldd $INSTALL_DIR/$OS/bin/shared/$DS_NAME
	rm /tmp/main_ds.out
	rm /tmp/fwd_ds.out
#	$INSTALL_DIR/$OS/bin/shared/$DS_NAME $DS_INST_NAME 1>/dev/null 2>&1 &
	$INSTALL_DIR/$OS/bin/shared/$DS_NAME $DS_INST_NAME 1>/tmp/main_ds.out 2>&1 &
	$INSTALL_DIR/$OS/bin/shared/$DS_NAME $DS_INST_NAME_2 1>/dev/null 2>&1 &
	sleep 5
#	$INSTALL_DIR/$OS/bin/shared/$FWD_DS_NAME $DS_INST_NAME 1>/dev/null 2>&1 &
	$INSTALL_DIR/$OS/bin/shared/$FWD_DS_NAME $DS_INST_NAME 1>/tmp/fwd_ds.out 2>&1 &
#	nohup $INSTALL_DIR/$OS/bin/shared/$DS_NAME $MCAST_INST_NAME 1>/dev/null 2>&1 &
fi

#
# Start test
#

if [ $OS_TYPE = "Windows" ]
then
	INSTALL_DIR=$WIN_INSTALL_DIR
fi

./runtest jenkins $INSTALL_DIR
RET=$?

#
# kill the servers
#

PIDS=`ps -e | grep $DS_NAME | grep -v grep | grep -v $MCAST_INST_NAME | awk '{print $1}'`
IND=0
for pid in $PIDS
do
	echo "Killing process with PID $pid"
	kill $pid
	let IND=IND+1
	if [ $IND == 2 ]
	then
		break
	fi
done

PIDS=`ps -e | grep $FWD_DS_NAME | grep -v grep | grep -v $MCAST_INST_NAME | awk '{print $1}'`
IND=0
for pid in $PIDS
do
	echo "Killing process with PID $pid"
	kill $pid
	let IND=IND+1
	if [ $IND == 1 ]
	then
		break
	fi
done

exit $RET
